package com.qianbao.qianbaobusiness.ui.support;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidbase.utils.RxUtils;
import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.Global;
import com.baseandroid.event.MerchantActivityEvent;
import com.baseandroid.gaode.GaoDeMap;
import com.baseandroid.mvp.BaseMvpFragment;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.utils.MoneyUtil;
import com.qianbao.qianbaobusiness.BuildConfig;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.UserPermession;
import com.qianbao.qianbaobusiness.ui.support.businessanalysis.BusinessAnalysisActivity;
import com.qianbao.qianbaobusiness.ui.webview.LoanWebviewActivity;
import com.qianbao.qianbaobusiness.ui.webview.SimpleWebViewActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tencent.stat.StatService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: Routee
 * @date 2018/6/11
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class SupportFragment extends BaseMvpFragment<SupportView, SupportPresenter> implements SupportView {

    @BindView(R.id.tv_title)
    TextView     mTvTitle;
    @BindView(R.id.iv_back_toolbar)
    ImageView    mIvBackToolbar;
    @BindView(R.id.tv_zcd_desc)
    TextView     mTvZcdDesc;
    @BindView(R.id.tv_zcd_money)
    TextView     mTvZcdMoney;
    @BindView(R.id.tv_zcd_add)
    TextView     mTvZcdAdd;
    @BindView(R.id.rv_increment)
    RecyclerView mRvIncrement;
    @BindView(R.id.ll_zcd)
    LinearLayout mLlZcd;
    private IncrementAdapter mIncrementAdapter;
    private String           mMerchantCollegeUrl;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_support;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void setupView(View rootView) {
        mIvBackToolbar.setVisibility(View.GONE);
        mTvTitle.setText("商家服务");
        mRvIncrement.setLayoutManager(new GridLayoutManager(getContext(), 2));
        getPresenter().getMerchantCollegeUrl();
        initIncrementRv();
        initZcd();
    }

    private void initZcd() {
        if (Global.getShopListInfo().loanInfo != null && Global.getShopListInfo().loanInfo
                                                                 .isEnable() && Global.getShopListInfo().getLoanMerchantInfo() != null) {
            for (UserPermession userPermession : Global.getShopListInfo()
                                                         .getLoanMerchantInfo().menuList) {
                if (TextUtils.equals("M100801", userPermession.menuCode)) {
                    mLlZcd.setVisibility(View.VISIBLE);
                    mTvZcdDesc.setText(Global.getShopListInfo().loanInfo.getDesc());
                    String amount = Global.getShopListInfo().loanInfo.getAmount();
                    amount = getString(R.string.rmb) + MoneyUtil.parseMoneyWithComma(amount);
                    mTvZcdMoney.setText(amount);
                    break;
                }
            }
        }
    }

    private void initIncrementRv() {
        if (mIncrementAdapter == null) {
            mIncrementAdapter = new IncrementAdapter(getContext());
        }
        mRvIncrement.setAdapter(mIncrementAdapter);
        List<String> objects = new ArrayList<>();
        objects.add("电子发票");
        objects.add("客户经理");
        if (Global.getMerchantActivityBean().getIsShow().equals("02")) {
            objects.add("零费率优惠");
        }
        if (Global.getWechatAppInfos().size() > 0) {
            //objects.add("微信小程序");
        }
        mIncrementAdapter.resetData(objects);
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.iv_add, R.id.tv_zcd_apply, R.id.rl_business_data, R.id.rl_merchant_college})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_add:
                break;
            case R.id.tv_zcd_apply:
                if (Global.mbLocSuccess) {
                    LoanWebviewActivity.gotoLoanWebView(getActivity(), Global.getUserInfoBean()
                                                                               .getLoanAccessToken(), "loan_apply", "商户贷", "support");
                    if (!BuildConfig.DEBUG) {
                        StatService.trackCustomEvent(Global.getContext(), "3_0_support_zcd");
                    }
                } else {
                    new RxPermissions(getActivity()).request(Manifest.permission.ACCESS_FINE_LOCATION)
                            .compose(RxUtils.<Boolean>applySchedulersLifeCycle(getMvpView()))
                            .subscribe(new RxObserver<Boolean>() {
                                @Override
                                public void onNext(Boolean granted) {
                                    if (granted) {
                                        GaoDeMap.getInstance().startMap();
                                    } else {
                                        ToastUtils.showShortToast("定位中...");
                                    }
                                }
                            });
                }
                break;
            case R.id.rl_business_data:
                Intent analysisIntent = new Intent(getActivity(), BusinessAnalysisActivity.class);
                startActivity(analysisIntent);
                if (!BuildConfig.DEBUG) {
                    StatService.trackCustomEvent(Global.getContext(), "3_0_support_analysis");
                }
                break;
            case R.id.rl_merchant_college:
                if (TextUtils.isEmpty(mMerchantCollegeUrl)) {
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString(SimpleWebViewActivity.WEBVIEW_TITLE, "商家学院");
                bundle.putString(SimpleWebViewActivity.WEBVIEW_URL, mMerchantCollegeUrl);
                Intent intent = new Intent(getActivity(), SimpleWebViewActivity.class);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
                if (!BuildConfig.DEBUG) {
                    StatService.trackCustomEvent(Global.getContext(), "3_0_support_college");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void getMerchantCollegeUrlSuccess(String str) {
        mMerchantCollegeUrl = str;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MerchantActivityEvent event) {
        initIncrementRv();
    }
}
